﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using AMG.DALProfile;

namespace AMG.Web
{
    public partial class WebForm1 : AMG.Web.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                doreg();
            }
        }

        private void doreg()
        {
            try
            {
                AMG.Model.AMG_UserInfo usinfo = new AMG.Model.AMG_UserInfo();
                usinfo.UserName = Request.Form["name"];
                if (Request.Form["sex"] == "0")
                {
                    usinfo.Sex = false;
                }
                else if (Request.Form["sex"] == "1")
                {
                    usinfo.Sex = true;
                }
                usinfo.City = "";
                usinfo.CustomerProperty = "1";
                usinfo.Company = Request.Form["company"];
                usinfo.Positison = Request.Form["Position"];
                usinfo.CRNo = Request.Form["crno"];
                if (Request.Form["DriverLisecen"] == "0")
                {
                    usinfo.DriverLisecen = false;
                }
                else if (Request.Form["DriverLisecen"] == "1")
                {
                    usinfo.DriverLisecen = true;
                }
                usinfo.Phone = Request.Form["Phone"];
                usinfo.Address = Request.Form["Address"];
                usinfo.ZipCode = Request.Form["ZipCode"];
                usinfo.Mail = Request.Form["mail"];
                if (Request.Form["IsBook"] == "0")
                {
                    usinfo.IsBook = false;
                }
                else if (Request.Form["IsBook"] == "1")
                {
                    usinfo.IsBook = true;
                }
                usinfo.CarNo = int.Parse(Request.Form["carno1"]);
                usinfo.ArrivalDate = null;
                usinfo.ArrivalTraffic = 0;
                usinfo.TrafficNo = "";
                usinfo.StartH = "";
                usinfo.StartM = "";
                usinfo.EndH = "";
                usinfo.EndM = "";
                usinfo.StartCity = "";
                usinfo.NeedGet = false;
                usinfo.ReturnDate = null;
                usinfo.ReturnTraffic =0;
                usinfo.TrafficNo1 = "";
                usinfo.ReturnH = "";
                usinfo.ReturnM = "";
                usinfo.ReturnCity = "";
                usinfo.NeedSend = false;
                usinfo.Room = false;
                usinfo.Golf = false;
                usinfo.Equipment = false;
                usinfo.CompanyID = UserID;
                usinfo.ID = int.Parse(Request.Form["ID"]);
                usinfo.RoundNo = int.Parse(Request.Form["ddlRound"]);

                AMG.DALSQLServer.AMG_UserInfo us = new AMG.DALSQLServer.AMG_UserInfo();
                us.Update(usinfo);
                if (Request.Form["IsBook"] == "1")
                {
                    AMG.Model.AMG_Book bookinfo = new AMG.Model.AMG_Book();
                    AMG.DALSQLServer.AMG_Book ab = new AMG.DALSQLServer.AMG_Book();
                    DbHelper.ExecuteNonQuery(CommandType.Text, "delete from AMG_Book where UserID=" + usinfo.ID + "", null);
                    if (Request.Form["C63"] != null)
                    {                        
                        bookinfo.CarID = 1;
                        bookinfo.UserID = usinfo.ID;
                        ab.Add(bookinfo);
                    }
                    if (Request.Form["SLK55"] != null)
                    {
                        bookinfo.CarID = 2;
                        bookinfo.UserID = usinfo.ID;
                        ab.Add(bookinfo);
                    }
                    if (Request.Form["SL63"] != null)
                    {
                        bookinfo.CarID = 3;
                        bookinfo.UserID = usinfo.ID;
                        ab.Add(bookinfo);
                    }
                }
                if (Request.Form["carno1"] != "0" || Request.Form["carno1"] != null)
                {
                    DbHelper.ExecuteNonQuery(CommandType.Text, "delete from AMG_cars where UserID=" + usinfo.ID + "", null);
                    cars(usinfo.ID);
                }
            }
            catch(Exception ex)
            {
                Response.Redirect("/editfail.aspx?r=" + Request.Form["ID"]);
            }
            Response.Redirect("/editok.aspx");

        }

        private void cars(int userid)
        {
            AMG.Model.AMG_Cars carsinfo = new AMG.Model.AMG_Cars();
            AMG.DALSQLServer.AMG_Cars ac = new AMG.DALSQLServer.AMG_Cars();
            for (int i = 1; i <= int.Parse(Request.Form["carno1"]); i++)
            {
                carsinfo.CarBland = Request.Form["carbland" + i];
                carsinfo.CarName = Request.Form["carname" + i];
                carsinfo.UserID = userid;
                ac.Add(carsinfo);
            }
        }
    }
}
